package com.redheart.web.controller.sys;

import com.redheart.common.utils.R;
import com.redheart.common.validator.Assert;
import com.redheart.common.validator.ValidatorUtils;
import com.redheart.common.validator.group.AddGroup;
import com.redheart.web.entity.SysUserEntity;
import com.redheart.web.response.BaseResponse;
import com.redheart.web.service.SysUserRoleService;
import com.redheart.web.service.SysUserService;
import com.redheart.web.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {

    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:user:list")
    public ResponseEntity<BaseResponse<Page<SysUserEntity> >> list(@RequestParam Pageable pageable){

        try {
            Page<SysUserEntity> allSysUser = sysUserService.getAllSysUser(pageable);
            BaseResponse<Page<SysUserEntity>> responseData = new BaseResponse<>();
            if (responseData != null && !responseData.getData().isEmpty()) {
                responseData.setCode(200);
                responseData.setMessage("success");
                responseData.setData(allSysUser);
                return new ResponseEntity<>(responseData, HttpStatus.OK);
            }
            else
            {
                responseData.setCode(400);
                responseData.setMessage("查询参数错误");
                return new ResponseEntity<>(responseData, HttpStatus.OK);
            }
        } catch (Exception e) {
            return  new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 获取登录的用户信息
     * @return
     */
    @RequestMapping("/info")
    public ResponseEntity<BaseResponse<SysUserEntity>> info()
    {
        try {
            BaseResponse<SysUserEntity> responseData=new BaseResponse<>();
            responseData.setCode(200);
            responseData.setMessage("success");
            responseData.setData(getUser());
            return new ResponseEntity<>(responseData,HttpStatus.OK);
        } catch (Exception e) {
            BaseResponse<SysUserEntity> responseData=new BaseResponse<>();
            responseData.setCode(400);
            responseData.setMessage(e.getMessage());
            return new ResponseEntity<>(responseData, HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 修改登录用户密码
     */
//    @SysLog("修改密码")
//    @RequestMapping("/password")
//    public R password(String password, String newPassword){
//        Assert.isBlank(newPassword, "新密码不为能空");
//
//        //原密码
//        password = ShiroUtils.sha256(password, getUser().getSalt());
//        //新密码
//        newPassword = ShiroUtils.sha256(newPassword, getUser().getSalt());
//
//        //更新密码
//        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
//        if(!flag){
//            return R.error("原密码不正确");
//        }
//
//        return R.ok();
//    }

    /**
     * 用户信息
     */
//    @RequestMapping("/info/{userId}")
//    @RequiresPermissions("sys:user:info")
//    public R info(@PathVariable("userId") Long userId){
//        SysUserEntity user = sysUserService.selectById(userId);
//
//        //获取用户所属的角色列表
//        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
//        user.setRoleIdList(roleIdList);
//
//        return R.ok().put("user", user);
//    }

    /**
     * 保存用户
     */
//    @SysLog("保存用户")
//    @RequestMapping("/save")
//    @RequiresPermissions("sys:user:save")
//    public R save(@RequestBody SysUserEntity user){
//        ValidatorUtils.validateEntity(user, AddGroup.class);
//
//        sysUserService.save(user);
//
//        return R.ok();
//    }

    /**
     * 修改用户
     */
//    @SysLog("修改用户")
//    @RequestMapping("/update")
//    @RequiresPermissions("sys:user:update")
//    public R update(@RequestBody SysUserEntity user){
//        ValidatorUtils.validateEntity(user, UpdateGroup.class);
//
//        sysUserService.update(user);
//
//        return R.ok();
//    }

    /**
     * 删除用户
     */
//    @SysLog("删除用户")
//    @RequestMapping("/delete")
//    @RequiresPermissions("sys:user:delete")
//    public R delete(@RequestBody Long[] userIds){
//        if(ArrayUtils.contains(userIds, 1L)){
//            return R.error("系统管理员不能删除");
//        }
//
//        if(ArrayUtils.contains(userIds, getUserId())){
//            return R.error("当前用户不能删除");
//        }
//
//        sysUserService.deleteBatchIds(Arrays.asList(userIds));
//
//        return R.ok();
//    }

}
